<?php
session_start();

// 定义函数：检查并初始化文件
function ensureFile($filename, $defaultContent = '[]') {
    if (!file_exists($filename)) {
        file_put_contents($filename, $defaultContent);
    }
    $content = file_get_contents($filename);
    $data = json_decode($content, true);
    return is_array($data) ? $data : json_decode($defaultContent, true);
}

// 验证用户是否登录
if (!isset($_SESSION['username'])) {
    header('Location: login.php'); // 跳转到登录页面
    exit();
}
require_once 'track_online.php';
// 当前用户
$currentUser = $_SESSION['username'];

// 群聊文件路径
$groupFile = __DIR__ . '/ql.json';

// 确保群聊文件存在并初始化
$groups = ensureFile($groupFile);

// 处理表单提交
$errorMessage = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['group_name'])) {
    $groupName = trim($_POST['group_name']);

    // 验证群聊名称
    if (empty($groupName)) {
        $errorMessage = '群聊名称不能为空！';
    } elseif (mb_strlen($groupName, 'UTF-8') > 10) {
        $errorMessage = '群聊名称不能超过10个字！';
    } else {
        // 检查群聊是否重名
        foreach ($groups as $group) {
            if ($group['name'] === $groupName) {
                $errorMessage = '群聊名称已存在，请选择其他名称！';
                break;
            }
        }

        // 如果没有错误，写入群聊文件
        if (empty($errorMessage)) {
            $groupId = md5(uniqid($groupName, true)); // 生成唯一的群聊ID
            $groups[] = [
                'id' => $groupId,
                'name' => $groupName,
                'creator' => $currentUser,
                'created_at' => date('Y-m-d H:i:s')
            ];
            file_put_contents($groupFile, json_encode($groups));
            header('Location: index.php?message=群聊创建成功'); // 跳转到主页或提示成功
            exit();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建群聊 - JIILCHAT</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f9f9f9;
        }
        .form-container {
            max-width: 400px;
            margin: 50px auto;
            background: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
        }
        .form-container h1 {
            margin-bottom: 20px;
            text-align: center;
        }
        .form-container input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .form-container button {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .form-container button:hover {
            background-color: #45a049;
        }
        .form-container p {
            margin-top: 10px;
            font-size: 14px;
            color: #666;
            text-align: center;
        }
        .error {
            color: red;
            margin-bottom: 10px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <h1>创建群聊</h1>
        <?php if (!empty($errorMessage)): ?>
            <p class="error"><?= htmlspecialchars($errorMessage) ?></p>
        <?php endif; ?>
        <form method="POST">
            <input type="text" name="group_name" placeholder="请输入群聊名称" maxlength="10" required>
            <button type="submit">创建</button>
        </form>
        <p>群聊创建后不可更改名称</p>
    </div>
</body>
</html>